package com.livestocksolutions.ui;

import java.util.Date;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

/* -- Version History --
 *  1.0 : 06 September 2012 - Initial version.
 */

/**
 * A base class to create the layout for a Paddock Object
 * @author Adam Parr
 * @version 1.0
 * 
 * @.modified 19 October 2012
 */
public class ViewPaddockArrayAdapter extends ViewLocationPaddockArrayAdapter {
	/**
	 * Size of the allowable array passed
	 */
	public static final int SIZE = 6;
	/**
	 * The area for each position within the String[position][{@value #AREA}] array passed in the
	 * Constructor.
	 */
	public static final int AREA = 3;
	/**
	 * The type of the area for each position within the String[position][{@value #AREATYPE}] array
	 * passed in the Constructor.
	 */
	public static final int AREATYPE = 4;
	/**
	 * Date of when the Paddock will be able to hold cattle area for each position within the
	 * String[position][{@value #WITHHOLDDATE}] array passed in the Constructor.
	 */
	public static final int WITHHOLDDATE = 5;
	
	private static String[] array;
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Paddocks to represent in the ListView.
	 */
	public ViewPaddockArrayAdapter(BaseActivity context, String[] values) {
		super(context, new String[][]{values});
		if(array==null){
			array = context.getResources().getStringArray(R.array.area_type);
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(textViewResourceId==0){
			setView(R.layout.item_paddock_view);
		}
		
		final View rowView = super.getView(position, convertView, parent);
		
		TextView area = (TextView) rowView.findViewById(R.id.area);
		View areaType = rowView.findViewById(R.id.areaMeasurement);
		TextView witholdPeriodState = (TextView) rowView.findViewById(R.id.safeGrazeDate);
		
		area.setText(values[position][AREA]);
		
		// Check if this date is empty, or before today.
		Date withholdDate = context.parse(values[position][WITHHOLDDATE]);
		witholdPeriodState.setText(withholdDate==null||withholdDate.before(new Date())?
				context.getResources().getString(R.string.paddock_withhold_safe)
			:
				context.getResources().getString(R.string.paddock_withhold_notSafe,context.format(withholdDate))
		);
		
		switch(textViewResourceId){
			case R.layout.item_paddock_modify:
				area.addTextChangedListener(new TextWatcher() {
					public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
					public void onTextChanged(CharSequence s, int start, int before, int count) {}
		
					public void afterTextChanged(Editable s) {
						//TODO add validation
						values[position][AREA]=s.toString();
					}
				});
				
				((Spinner)areaType).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent, View view, int index, long id) {
						values[position][AREATYPE]=String.valueOf(index);
					}
		
					public void onNothingSelected(AdapterView<?> parent) {}
				});
				
				// Determine the type of area this is
				((Spinner)areaType).setSelection(Integer.parseInt(values[position][AREATYPE]));
			break;
			case R.layout.item_paddock_view:
				((TextView)areaType).setText(array[Integer.parseInt(values[position][AREATYPE])]);
		}
		return rowView;
	}
	
	/**
	 * Display the View that will allow the modification of some fields.<br>
	 * This should be used when choosing to modify the Paddock.
	 */
	public void setModifyPaddock(){
		setView(R.layout.item_paddock_modify);
	}
	
	/**
	 * Display the View that will only display the Paddock details.
	 */
	public void setViewPaddock(){
		setView(R.layout.item_paddock_view);
	}
}
